<template>
  <a-form
    :rules="dataRules"
    :model="dataForm"
    @finish="submit"
    :wrapper-col="{ offset: 1, span: 12 }"
    :label-col="{ span: 4, offset: 2 }"
    :disabled="hasPerm('system:dict:edit')"
  >
    <FormCard :back="back" title="编辑/新增">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="loading" block>保存</a-button>
      </template>

      <a-form-item label="字典名称" name="dictLabel">
        <a-input placeholder="请输入字典标签" v-model:value="dataForm.dictLabel"></a-input>
      </a-form-item>
      <a-form-item label="字典键值" name="dictValue">
        <a-input placeholder="请输入字典键值" v-model:value="dataForm.dictValue"></a-input>
      </a-form-item>
      <a-form-item label="附加值" name="addition">
        <a-textarea placeholder="请输入字典键值" v-model:value="dataForm.addition"></a-textarea>
      </a-form-item>

      <a-form-item label="字典类型" name="dictType">
        <a-input disabled placeholder="请输入字典类型" v-model:value="dataForm.dictType"></a-input>
      </a-form-item>
      <a-form-item label="字典编码" name="dictCode">
        <a-input-number
          :min="0"
          placeholder="请输入字典编码"
          class="w-[100%]"
          v-model:value="dataForm.dictCode"
        ></a-input-number>
      </a-form-item>
      <a-form-item label="字典排序" name="dictSort">
        <Sort v-model="dataForm.dictSort!" :total="dataTable.query.total"></Sort>
      </a-form-item>

      <a-form-item label="是否默认" name="isDefault">
        <a-switch
          :checked-value="'Y'"
          :un-checked-value="'N'"
          :options="isOptions"
          v-model:checked="dataForm.isDefault"
        ></a-switch>
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="dataForm.status" :options="statusOptions"></a-radio-group>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea placeholder="请输入备注" v-model:value="dataForm.remark"></a-textarea>
      </a-form-item>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import { createDictData, updateDictData } from '@/api/modules/system/dict/dict.data';
import { isOptions } from '@/global/options/system';
import { hasPerm } from '@/utils/common/perm';
import { response } from '@/utils/table/table';
import Sort from '@/views/components/sort/Sort.vue';
import { dataList } from '../../data/curd';
import { dataForm, dataRules, dataShowForm } from '../../data/form';
import { statusOptions } from '../../data/options';
import { dataTable } from '../../data/table';

const loading = ref(false);

const submit = async () => {
  loading.value = true;
  if (dataForm.value.dictCode) {
    await response(updateDictData, dataForm.value);
  } else {
    await response(createDictData, dataForm.value);
  }
  await dataList();
  loading.value = false;
  back();
};

const back = () => {
  dataShowForm.value = false;
};
</script>

<style lang="scss" scoped></style>
